package online.westonline.com.westonline.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.easy.west.westclient.api.constacts.bean.PersonBean;
import com.easy.west.westclient.api.constacts.event.FindHumanByDepEvent;
import com.easy.west.westclient.api.department.bean.DepartMentInfo;
import com.easy.west.westclient.api.department.event.FindoneDepartMentEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import online.westonline.com.westonline.MyApplication;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.adapter.constacts.PopConstactAndDepartEmentAdpater;
import online.westonline.com.westonline.base.BaseActivity;

/**
 * 部门详情activity
 */
public class DepInfoActivity extends BaseActivity {
    @BindView(R.id.tv_topbar_title)
    TextView tvTopbarTitle;
    @BindView(R.id.tv_topbar_title_center)
    TextView tvTopbarTitleCenter;
    @BindView(R.id.img_toolbar_menu)
    ImageView imgToolbarMenu;
    @BindView(R.id.tv_topbar_menu)
    TextView tvTopbarMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_depinfo_name)
    TextView tvDepinfoName;
    @BindView(R.id.recyclerView_depinfo_constactslist)
    RecyclerView recyclerViewDepinfoConstactslist;
    @BindView(R.id.tv_depinfo_count)
    TextView tvDepinfoCount;
    private int dep_id;
    private DepartMentInfo departMentInfo;
    private List<PersonBean> personBeanList = new ArrayList<>();
    private PopConstactAndDepartEmentAdpater constactAndDepartEmentAdpater;

    @Override
    protected void initview() {
        toolbar.setTitle("部门详情");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        initrecyclerview();
    }

    private void initrecyclerview() {
        constactAndDepartEmentAdpater = new PopConstactAndDepartEmentAdpater();
        recyclerViewDepinfoConstactslist.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewDepinfoConstactslist.setAdapter(constactAndDepartEmentAdpater);
    }

    @Override
    protected void initdata() {
        showProgessDialog("加载中");
        dep_id = getIntent().getExtras().getInt("id");
        //请求部门信息
        MyApplication.getClient().finddepartmentinfo(dep_id);
    }

    @Override
    protected int setlayout() {
        return R.layout.activity_dep_info;
    }

    /***
     * 查找部门
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getDepInfo(FindoneDepartMentEvent eva) {
        if (eva.isOk()) {
            departMentInfo = eva.getBean();
            MyApplication.getClient().findPeopleByDepartMent(dep_id);
            initui();
        } else {
            dissmissDialog();
        }
    }

    /**
     * 查找人员列表
     *
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getPeople(FindHumanByDepEvent eva) {
        if (eva.isOk()) {
            personBeanList = eva.getBean().getData();
            configrecycler();
            dissmissDialog();
        } else {
            dissmissDialog();
        }
    }

    private void configrecycler() {
        tvDepinfoCount.setText("共"+personBeanList.size()+"名员工");
        constactAndDepartEmentAdpater.setDatas(personBeanList, null, null);
        constactAndDepartEmentAdpater.notifyDataSetChanged();
    }

    private void initui() {
        tvDepinfoName.setText(departMentInfo.getData().getName());
        //注意统计人数
    }


}
